package com.linkq.ipinyin.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.linkq.core.base.BaseService;
import com.linkq.ipinyin.common.constants.GlobalData;
import com.linkq.ipinyin.common.utils.StringUtils;
import com.linkq.ipinyin.dao.FeedbackDao;
import com.linkq.ipinyin.mapper.FeedbackMapper;
import com.linkq.ipinyin.model.BcFile;
import com.linkq.ipinyin.model.Feedback;
import com.linkq.ipinyin.utils.FileUtils;
import com.linkq.ipinyin.vo.ResultInfo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @FileName FeedBackService
 * @Description
 * @Author wangpeng
 * @date 2025-03-01
 **/

@Service
@Transactional
public class FeedbackService extends BaseService<FeedbackDao, Feedback> {
    @Autowired
    private FileService fileService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private FeedbackMapper feedbackMapper;

    public ResultInfo submitFedBack(Feedback feedBack, List<MultipartFile> files) {
        // 存储文件
        List<Integer> fileIds = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(files)) {
            for (MultipartFile file : files) {
                BcFile bcFile = FileUtils.upload(file);
                int fileId = fileService.insert(bcFile);
                fileIds.add(fileId);
            }
        }
        feedBack.setFeedbackTime(new Date());
        feedBack.setCustomerId(customerService.getUser().getId());
        feedBack.setStatus(GlobalData.FEEDBACK_STATUS_PENDIN);
        if (CollectionUtils.isNotEmpty(fileIds)) {
            feedBack.setFileIds(StringUtils.join(fileIds, ","));
        }
        if (insert(feedBack) > 0) {
            return ResultInfo.success(1, "提交反馈意见成功");
        }
        return ResultInfo.error("提交反馈意见失败");
    }

    public ResultInfo changeStatus(Feedback feedBack){
        if (null == feedBack.getId() || null == feedBack.getStatus()) {
            return ResultInfo.error("参数错误", -1);
        }
        Feedback origin = selectById(feedBack.getId());
        origin.setStatus(feedBack.getStatus());
        if (updateById(origin) > 0) {
            return ResultInfo.success(1, "更新状态成功");
        }
        return ResultInfo.error("更新状态失败", -2);
    }

    public ResultInfo getMyFeedback(Feedback feedback) {
        List<Feedback> feedbackList = feedbackMapper.selectList(
                Wrappers.<Feedback>lambdaQuery()
                        .eq(Feedback::getCustomerId, customerService.getUser().getId())
                        .like(StringUtils.isNotEmpty(feedback.getTitle()), Feedback::getTitle, feedback.getTitle())
                        .orderByDesc(Feedback::getFeedbackTime)
        );
        return ResultInfo.success(feedbackList);
    }
}
